// pages/interface/createcar/createcar.js
var common = require('../../../utils/common.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var username = app.globalData.username;
    that.setData({
      username: username
    });
  },

  // form表单提交事件
  formSubmit: function(event) {
    var that = this;
    var licence_plate = event.detail.value.licence_plate;
    var username = that.data.username;
    if (licence_plate != "" && username != "") {
      wx.request({
        url: 'https://car.shiliangproject.cn/car/registercar',
        data: {
          username: username,
          licence_plate: licence_plate
        },
        // 请求头
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        success: function (res) {
          console.log(res.data);
          if (res.data['res'] == 1) {
            common.showTip('创建成功！');
            setTimeout(function () {
              wx.redirectTo({
                url: '../carmanage/carmanage'
              });
            }, 2000);
          } else {
            common.showTip('创建失败！','loading');
          }
        }
      })
    } else {
      wx.showLoading({
        title: '车牌信息错误！',
      });
      setTimeout(function () {
        wx.hideLoading();
      }, 1500);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})